package com.data.modules.jd.controller;


import com.alibaba.fastjson.JSON;
import com.data.common.api.vo.Result;
import com.data.common.system.util.JwtUtil;
import com.data.common.util.JwtIOTAuthUtil;
import com.data.modules.jd.entity.JdOutCard;
import com.data.modules.jd.entity.JdPushCard;
import com.data.modules.jd.service.IJdOutCardService;
import com.data.modules.jd.vo.InCarReqVo;
import com.data.modules.jd.vo.OutCarReqVo;
import com.data.modules.jd.vo.PackPaceReqVo;
import com.data.modules.jd.vo.PreinCarReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @Author boot
 */

@RestController
@RequestMapping("/car")
@Api(tags="car")
@Slf4j
@SuppressWarnings("ALL")
public class JdPushController<stringWarnType> {


	@Autowired
	private IJdOutCardService outCarService;

	@ApiOperation("车辆进场回调")
	@PostMapping("/v1/inPushCarInfo")
	public Map inPushCadInfo(@RequestBody InCarReqVo carReqVo) throws IOException, ServletException {
		Map map = new HashMap();
		Map resultMap = new HashMap();
		log.info("==车辆进场回调请求参数=,{}", JSON.toJSONString(carReqVo));
		JdOutCard jdPushCard = new JdOutCard();
		BeanUtils.copyProperties(carReqVo,jdPushCard);
		jdPushCard.setCreateBy("smVip030");
		outCarService.save(jdPushCard);

		map.put("resCode","0000");
		map.put("resMsg","成功");
		resultMap.put("data",map);
		return resultMap;
	}

	@ApiOperation("车辆预进场回调")
	@PostMapping("/v1/preinPushCarInfo")
	public Map preinPushCadInfo(@RequestBody PreinCarReqVo carReqVo) throws IOException, ServletException {
		Map map = new HashMap();
		Map resultMap = new HashMap();
		log.info("==车辆进场回调请求参数=,{}", JSON.toJSONString(carReqVo));
		JdOutCard jdPushCard = new JdOutCard();
		BeanUtils.copyProperties(carReqVo,jdPushCard);
		jdPushCard.setCreateBy("smVip030");
		jdPushCard.setArriveTime(carReqVo.getPreArriveTime());
		outCarService.save(jdPushCard);

		map.put("resCode","0000");
		map.put("resMsg","成功");
		resultMap.put("data",map);
		return resultMap;
	}


	@ApiOperation("车辆出场回调")
	@PostMapping("/v1/outPushCarInfo")
	public Map outPushCadInfo(@RequestBody OutCarReqVo carReqVo) throws IOException, ServletException {
		Map map = new HashMap();
		Map resultMap = new HashMap();
		log.info("==车辆出场回调请求参数=,{}", JSON.toJSONString(carReqVo));
		JdOutCard jdPushCard = new JdOutCard();
		BeanUtils.copyProperties(carReqVo,jdPushCard);
		jdPushCard.setCreateBy("smVip030");
		outCarService.save(jdPushCard);

		map.put("resCode","0000");
		map.put("resMsg","成功");
		resultMap.put("data",map);
		return resultMap;
	}

	@ApiOperation("车辆预出场回调")
	@PostMapping("/v1/preOutPushCarInfo")
	public Map preOutPushCadInfo(@RequestBody OutCarReqVo carReqVo) throws IOException, ServletException {
		Map map = new HashMap();
		Map resultMap = new HashMap();
		log.info("==车辆预出场回调请求参数=,{}", JSON.toJSONString(carReqVo));
		JdOutCard jdPushCard = new JdOutCard();
		BeanUtils.copyProperties(carReqVo,jdPushCard);
		jdPushCard.setCreateBy("smVip030");
		outCarService.save(jdPushCard);

		map.put("resCode","0000");
		map.put("resMsg","成功");
		resultMap.put("data",map);
		return resultMap;
	}


	@ApiOperation("剩余车位控制")
	@PostMapping("/v1/spaceInfo")
	public Map spaceInfo( @RequestBody PackPaceReqVo carReqVo) throws IOException, ServletException {
		Map map = new HashMap();
		Map resultMap = new HashMap();
		log.info("==剩余车位控制请求参数={}", JSON.toJSONString(carReqVo));
		map.put("resCode","0000");
		map.put("resMsg","成功");
		resultMap.put("data",map);
		return resultMap;
	}

	@ApiOperation("获取车辆统计数据")
	@GetMapping("/v1/queryCarInfo")
	public Result<?> queryCarInfo(HttpServletRequest req)  throws Exception {

		Result result = new Result();
		Integer code = 0;
		String username = JwtUtil.getUserNameByToken(req);
		//统计
		Map map = outCarService.queryCarInfoByUserName(username);
		return Result.OK(map);
	}


	@ApiOperation("获取车辆进出前20条数据")
	@GetMapping("/v1/queryInOutCarInfo")
	public Result<?> queryInOutCarInfo(HttpServletRequest req)  throws Exception {

		String username = JwtUtil.getUserNameByToken(req);
		//统计
		List<Map> list = outCarService.queryInOutCarInfo(username);
		return Result.OK(list);
	}


}
